package com.pac.hospital.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.tools.StringTools;
import com.common.tools.TimeTools;
import com.common.util.ErrorNumber;
import com.common.util.LogicException;
import com.pac.hospital.bean.HospitalDepartmentBean;
import com.pac.hospital.dao.HospitalDepartmentDao;
import com.pac.hospital.service.HospitalDepartmentService;

/**
 * 医院科室Service实现
 * 
 * @author lujun.chen
 * @version [版本号, 2015年3月9日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service(value = "hospitalDepartmentService")
public class HospitalDepartmentServiceImpl implements HospitalDepartmentService {

	private static Logger logger = Logger
			.getLogger(HospitalDepartmentServiceImpl.class);

	@Autowired
	private HospitalDepartmentDao hospitalDepartmentDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public String addHospitalDepartment(
			HospitalDepartmentBean hospitalDepartmentBean) {

		if (null == hospitalDepartmentBean) {
			logger.error("添加医院科室信息出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(hospitalDepartmentBean.getName())) {
			logger.error("添加医院科室信息出错，科室名称为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "科室名称为空");
		}

		if (StringTools.isNull(hospitalDepartmentBean.getHospitalId())) {
			logger.error("添加医院科室信息出错，医院ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "医院ID为空");
		}

		hospitalDepartmentBean.setCreateDate(TimeTools.now());

		return hospitalDepartmentDao
				.addHospitalDepartment(hospitalDepartmentBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateHospitalDepartment(
			HospitalDepartmentBean hospitalDepartmentBean) {

		if (null == hospitalDepartmentBean) {
			logger.error("更新医院科室信息出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(hospitalDepartmentBean.getId())) {
			logger.error("更新医院科室信息出错，科室ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "科室ID为空");
		}

		return hospitalDepartmentDao
				.updateHospitalDepartment(hospitalDepartmentBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delHospitalDepartment(
			HospitalDepartmentBean hospitalDepartmentBean) {
		if (null == hospitalDepartmentBean) {
			logger.error("删除医院科室信息出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(hospitalDepartmentBean.getId())) {
			logger.error("删除医院科室信息出错，ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "科室ID为空");

		}

		return hospitalDepartmentDao
				.delHospitalDepartment(hospitalDepartmentBean);
	}

	@Override
	public List<HospitalDepartmentBean> queryHospitalDepartment(
			HospitalDepartmentBean hospitalDepartmentBean) {

		return hospitalDepartmentDao
				.queryHospitalDepartment(hospitalDepartmentBean);
	}

	@Override
	public HospitalDepartmentBean findHospitalDepartment(
			HospitalDepartmentBean hospitalDepartmentBean) {
		if (null == hospitalDepartmentBean) {
			return null;
		}

		List<HospitalDepartmentBean> departmentList = hospitalDepartmentDao
				.queryHospitalDepartment(hospitalDepartmentBean);
		if ((null != departmentList) && (departmentList.size() > 0)) {
			return departmentList.get(0);
		}

		return null;
	}

}
